<template>
  <div>
    <mbao-x item1="权限管理" item2="角色列表"></mbao-x>
    <el-card class="Top">
      <el-button type="primary" @click="OpenRole">添加角色</el-button>
      <el-table :data="tableData" border style="width: 100%" class="Top">
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row v-for="(item, index) in scope.row.children" :key="index">
              <!-- 一级 -->
              <el-col :span="5">
                <el-tag
                  effect="dark"
                  closable
                  :hit="true"
                  class="Tag"
                  @close="handleClose(scope.row, item.id)"
                >
                  {{ item.authName }}
                </el-tag>
              </el-col>
              <!-- 二级三级 -->
              <el-col :span="19"
                ><div class="grid-content bg-purple-light">
                  <el-row
                    v-for="(item1, i) in item.children"
                    :key="i"
                    @close="handleClose(scope.row, item1.id)"
                  >
                    <el-col :span="6">
                      <el-tag effect="dark" closable type="success" class="Tag">
                        {{ item1.authName }}
                      </el-tag>
                    </el-col>
                    <el-col :span="18">
                      <el-tag
                        effect="dark"
                        closable
                        type="warning"
                        class="Tag"
                        v-for="(item2, isv) in item1.children"
                        :key="isv"
                        @close="handleClose(scope.row, item2.id)"
                      >
                        {{ item2.authName }}
                      </el-tag>
                    </el-col>
                  </el-row>
                </div></el-col
              >
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="roleName" label="角色名称"> </el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              icon="el-icon-edit"
              type="primary"
              @click="OpenEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="Del(scope.row)"
              >删除</el-button
            >
            <el-button
              size="mini"
              type="warning"
              icon="el-icon-s-tools"
              @click="Role(scope.row)"
              >分配权限</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 添加编辑角色对话框 -->
      <el-dialog :title="names" :visible.sync="dialogFormVisibles">
        <el-form :model="form" :rules="rules" ref="form">
          <el-form-item
            label="角色名称"
            :label-width="formLabelWidth"
            prop="roleName"
          >
            <el-input v-model="form.roleName"></el-input>
          </el-form-item>

          <el-form-item
            label="角色描述"
            :label-width="formLabelWidth"
            prop="roleDesc"
          >
            <el-input v-model="form.roleDesc"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisibles = false">取 消</el-button>
          <el-button type="primary" @click="AddRole('form')">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 分配权限 -->
      <el-dialog :title="names" :visible.sync="dialogFormVisible">
        <el-tree
          :data="data"
          show-checkbox
          node-key="id"
          :default-checked-keys="keys"
          :props="defaultProps"
          ref="treeRef"
        >
        </el-tree>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="ADD">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import MbaoX from '../components/MbaoX.vue'
export default {
  components: {
    MbaoX
  },
  data() {
    return {
      form: {
        roleName: '',
        roleDesc: ''
      },
      rules: {
        roleName: [
          { required: true, message: '请输入角色名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        roleDesc: [
          { required: true, message: '请输入角色描述', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      },
      names: '',
      EditID: '',
      keys: [],

      formLabelWidth: '80px',
      dialogFormVisibles: false,
      dialogFormVisible: false,
      tableData: [
        {
          username: '111'
        }
      ],
      defaultProps: {
        children: 'children',
        label: 'authName'
      },
      data: []
    }
  },
  created() {
    this.getlist()
  },
  mounted() {},
  methods: {
    async getlist() {
      const res = await this.$http.Role()
      console.log(res)
      this.tableData = res
    },
    OpenRole() {
      this.dialogFormVisibles = true
      this.names = '添加角色'
    },
    OpenEdit(row) {
      this.dialogFormVisibles = true
      this.names = '编辑角色'
      this.EditID = row.id
    },
    // 移除权限
    async handleClose(row, id) {
      // let Dels
      // try {
      //   Dels = await this.$confirm(
      //     '此操作将永久移除该权限, 是否继续?',
      //     '提示',
      //     {
      //       confirmButtonText: '确定',
      //       cancelButtonText: '取消',
      //       type: 'warning'
      //     }
      //   )
      // } catch (error) {
      //   Dels = error
      // }
      // if (Dels === 'confirm') {
        const res = await this.$http.Dels(row.id, id)
        console.log(res)
        // this.getlist()
      // }
    },
    // 确定
    AddRole(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          if (this.names === '添加角色') {
            const res = await this.$http.addRole(this.form)
            console.log(res)
          } else {
            const res = await this.$http.EditRole(this.EditID, this.form)
            console.log(res)
          }
          this.getlist()
          this.dialogFormVisibles = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 删除
    async Del(row) {
      let Dels
      try {
        Dels = await this.$confirm(
          '此操作将永久删除该文件, 是否继续?',
          '提示',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        )
      } catch (error) {
        Dels = error
      }
      if (Dels === 'confirm') {
        const res = this.$http.DelRole(row.id)
        console.log(res)
        this.getlist()
      }
    },
    async Role(row) {
      this.EditID = row.id
      this.keys = []
      this.dialogFormVisible = true
      const res = await this.$http.AllRole('tree')
      console.log(res)
      this.data = res
      this.GetById(row, this.keys)
    },
    GetById(node, arr) {
      if (!node.children) {
        return arr.push(node.id)
      }
      node.children.forEach((item) => this.GetById(item, arr))
    },
    async ADD() {
      const Arr = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys()
      ]
      const arr = Arr.join(',')
      const res = await this.$http.FenRole(this.EditID, { rids: arr })
      console.log(res)
      this.getlist()
      this.dialogFormVisible = false
    }
  }
}
</script>
<style scoped lang='scss'>
.Top {
  margin-top: 20px;
}
.Tag {
  margin: 10px;
}
</style>
